package com.example.mycontacts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class CategoryScreen extends Activity{
	private ListView listView;
	private String category;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_category_screen);		
		
		Intent myIntent = getIntent();
		category = myIntent.getStringExtra("category");
		TextView catName = (TextView) findViewById(R.id.categoryName);
		catName.setText(category);
		new PopulateContactCats().execute();

	      final Button button_home = (Button) findViewById(R.id.button_home);
	        button_home.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	finish();
	            }
	        });
	        
	        
	        
	        final Button button_edit = (Button) findViewById(R.id.button_edit);
	        button_edit.setOnClickListener(new View.OnClickListener()
	        {
	        	   	
	        	@Override
	        	public void onClick(View v) {
	        		// TODO Auto-generated method stub
	        		AlertDialog.Builder input = new AlertDialog.Builder(CategoryScreen.this);
	        		   input.setTitle("Change Category Name");

	        		   final EditText txt = new EditText(CategoryScreen.this);
	        		   input.setView(txt);
	        		   
	        		   input.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
	        			   public void onClick(DialogInterface dialog, int whichButton) {
	        				   String str = txt.getText().toString();
	        				   DBAdapter db = new DBAdapter(CategoryScreen.this);
	        				   db.open();
	        				   if(str.matches(""))
	        				   {
	        					   Toast.makeText(CategoryScreen.this, "No Input", Toast.LENGTH_SHORT).show();
	        				   }
	        				   else
	        				   {
	        					 db.changeCategory(category, str);
	        				   }
	        				   db.close();
	        				   Intent intent = new Intent(CategoryScreen.this, MainActivity.class);
	        	               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    
	        	               startActivity(intent);
	        				   dialog.cancel();
	        			   }
	        		   });
	        		   
	        		   input.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	        			   public void onClick(DialogInterface dialog, int whichButton) {
	        				   dialog.cancel();
	        			   }

	        	   });
	        		input.show();
	        	}
	        	   
	        });
	        
	        
	        
	        
	        
	        
	        
	        listView = (ListView) findViewById(R.id.list);
		    
			listView.setOnItemClickListener(new OnItemClickListener()		
			{
				public void onItemClick(AdapterView<?> parent, View view, int position, long id){
					TextView txt = (TextView)view.findViewById(R.id.listviewtext2);
					Intent intent = new Intent(CategoryScreen.this, ViewContact.class);
					intent.putExtra("userid", txt.getText().toString());
					startActivity(intent);
				}
			});
	        
	}
	    
	    public void onSearchRequest(View view){
			onSearchRequested();
		}
	
	  //get the names from the database to display them/add them to array
	  		private class PopulateContactCats extends AsyncTask<String, Object, List<Information>>
	  		{
	  			//the main screen java file goes inside of parameter 
	  			//DBAdapter is my database class
	  			DBAdapter dbAdapter = new DBAdapter(CategoryScreen.this);
	  			
	  			
	  			@Override
	  			protected List<Information> doInBackground(String...objects){
	  				dbAdapter.open();
	  				return dbAdapter.populateCategoryListView(category);
	  			}
	  			
	  			protected void onPostExecute(List<Information> result)
	  			{			
	  				listView = (ListView) findViewById(R.id.list);
	  				List<Map<String, String>> contacts = new ArrayList<Map<String, String>>();
	  				String[] rows = {"name", "id"};
	  				for(int i = 0; i < result.size(); i++)
	  				{
	  					HashMap<String, String> map = new HashMap<String, String>();
	  					//initialize row
	  					map.put("name", result.get(i).getName());
	  					map.put("id", result.get(i).getUserId());
	  					contacts.add(map);
	  				}
	  				
	  				SimpleAdapter adapter = new SimpleAdapter(CategoryScreen.this, contacts, R.layout.custome_row_view, rows, new int[] {R.id.listviewtext1, R.id.listviewtext2});
	  				listView.setAdapter(adapter);
	  				
	  				dbAdapter.close();
	  			}

	  		}    
	
	public void onListItemClick(ListView l, View v, int position, long id){
		//send to the view contact screen of the appropriate name
	}
}
